import winreg
import logging
from Login import *

##############
#author:张子路#
##############

class RegrystGet():

    def __init__(self):
        pass

    def RegistryValueGet(self,RegistryKeyDict):
        RootList=[]
        Result={}
        for iteam in RegistryKeyDict.keys():
            try:
                RootList.append([iteam,winreg.ConnectRegistry(None,iteam)])        #连接注册表
            except Exception as e:
                logging.error("注册表根目录链接失败出错 {0}".format(e), exc_info=True)
        print("--->开始读取自启动列表...")
        for iteam in RootList:
            try:
                for keyiteams in RegistryKeyDict[iteam[0]]:       #获取注册表项
                    count = 0
                    try:
                        key=winreg.OpenKey(iteam[1], keyiteams)
                        while(1):
                            try:
                                n, v, t = winreg.EnumValue(key, count)
                                Result[n] = v
                                count += 1
                            except EnvironmentError:
                                break
                            except Exception as e:
                                logging.error("注册表项获取失败出错 {0}".format(e), exc_info=True)
                    except Exception as e:
                        logging.error("注册表项获取失败出错 {0}".format(e), exc_info=True)
                    finally:
                        winreg.CloseKey(key)
            except Exception as e:
                logging.error("注册表项链接失败出错 {0}".format(e), exc_info=True)
        print("<---读取完毕，注册表共有%s个注册表项" %len(Result))
        try:
            return Result         #返回读取结果
        except Exception as e:
            logging.error("注册表值返回失败出错 {0}".format(e), exc_info=True)

    def RegistryKeyGet(self,RegistryKeyDict):
        RootList=[]
        Result=[]
        for iteam in RegistryKeyDict.keys():
            try:
                RootList.append([iteam,winreg.ConnectRegistry(None,iteam)])
            except Exception as e:
                logging.error("注册表根目录链接失败出错 {0}".format(e), exc_info=True)
        print("--->开始读取子健列表...")
        for iteam in RootList:
            try:
                for keyiteams in RegistryKeyDict[iteam[0]]:
                    count = 0
                    try:
                        key=winreg.OpenKey(iteam[1], keyiteams)          #打开注册表相应键值
                        while(1):
                            try:
                                reg = winreg.EnumKey(key, count)         #获取子健
                                Result.append(reg)
                                count += 1
                            except EnvironmentError:
                                break
                            except Exception as e:
                                logging.error("注册表子健获取失败出错 {0}".format(e), exc_info=True)
                    except Exception as e:
                        logging.error("注册表项子健失败出错 {0}".format(e), exc_info=True)
                    finally:
                        winreg.CloseKey(key)
            except Exception as e:
                logging.error("注册表项链接失败出错 {0}".format(e), exc_info=True)
        print("<---读取完毕，注册表共有%s个注册表子健" %len(Result))
        try:
            return Result         #输出结果
        except Exception as e:
            logging.error("注册表值返回失败 {0}".format(e), exc_info=True)

def printout(result):
    i = 1
    for k, v in result.items():
        print(i, "-->", k, "：", v)
        i += 1


if __name__=="__main__":
    login=LoginStart()
    login.start()
    '''
    reg=RegrystGet()
    d={winreg.HKEY_LOCAL_MACHINE:[r"SOFTWARE\Microsoft\Windows\CurrentVersion\Run",r"SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce"],
       winreg.HKEY_CURRENT_USER:[r"SOFTWARE\Microsoft\Windows\CurrentVersion\Run",r"SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce"]}
    val=reg.RegistryValueGet(d)
    printout(val)
    '''
